(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.glSystem.updateController', updateController);
	updateController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'glSystemService' ];
	function updateController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, glSystemService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.validatorConfig = validatorConfig;
		ctrl.glSystem = {};

		glSystemService.initPageCallback(ctrl);
		ctrl.init = function() {
			glSystemService.getGlSystemLayout('update').then(function(layoutRsponse) {
				if (!layoutRsponse.data) {
					return;
				}
				ctrl.layoutConfig = layoutRsponse.data;
				glSystemService.loadOptions().then(function(glSystemOptions) {
					var glSystem;
					glSystemService.initOptions(ctrl, glSystemOptions);
					authService.update(params).then(function(response) {
						glSystem = response.data;
						if (glSystem) {
							ctrl.intaccrmethodOption = glSystem.intaccrmethodList;
							ctrl.feeamortmethodOption = glSystem.feeamortMethodList;
							ctrl.pastduemethodOption = glSystem.pastdueMethodList;
							ctrl.glMgrOption = glSystem.glpostMgrList.data;
							ctrl.glSystem = glSystem;
				    	}
					});
				});
			});
		};
		
		ctrl.updateSave = function() {
			if (ctrl.glSystem.taskId) {
				return authService.updateContinueSave(ctrl.glSystem).then(saveSuccessful, saveFailed);
			} else {
				return authService.updateSave(ctrl.glSystem).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.updateSubmit = function() {
			if (ctrl.glSystem.taskId) {
				return authService.updateContinueSubmit(ctrl.glSystem).then(submitSuccessful, submitFailed);
			} else {
				return authService.updateSubmit(ctrl.glSystem).then(submitSuccessful, submitFailed);
			}
		};


		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.glSystem.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.glSystem.list',
					'state' : 'cda.glSystem.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
//			ctrl.glSystem = response.data.glSystem;
//			ctrl.glSystem.taskId = response.data.task.taskId;
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
